public class DeleteDuplicates2 {
    // 删除有序链表中重复的元素-II
    // https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        ListNode pHead = new ListNode(-1);
        ListNode cur = pHead;
        ListNode cur1 = head;
        while (cur1 != null) {
            ListNode tmp = cur1;
            cur1 = cur1.next;
            boolean flg = false;
            while (cur1 != null && cur1.val == tmp.val) {
                cur1 = cur1.next;
                flg = true;
            }
            if (!flg) {
                cur.next = tmp;
                cur = cur.next;
            }
        }
        cur.next = null;
        return pHead.next;
    }

    public static void main(String[] args) {
        DeleteDuplicates2 d = new DeleteDuplicates2();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next.next = new ListNode(5);
        System.out.println(d.deleteDuplicates(head));
    }
}
